const app = getApp()
let qCloud = require("../../index.js");

Page({
  /** 页面的初始数据 */
  data: {
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
    cooperateEnterpriseId: 0, // 合作企业
    cooperateEnterpriseName: "", // 合作企业名称
    cooperateEnterpriseList: [], // 合作企业列表
    customerName: "", // 客户名称
    customerAddress: "", // 客户地址
    customerPhone: "", // 客户电话
  },
  // 搜索
  searchInput(e) {},
  // 添加合作企业
  addTap(e) {},
  // 单选框选择
  radioChange(e) {
    let self = this;

    let cooperateEnterpriseId = ""; // 合作企业
    let cooperateEnterpriseName = ""; // 合作企业名称
    let customerName = ""; // 客户名称
    let customerAddress = ""; // 客户地址
    let customerPhone = ""; // 客户电话

    let cooperateEnterpriseList = self.data.cooperateEnterpriseList;
    for (let i = 0; i < cooperateEnterpriseList.length; i++) {
      cooperateEnterpriseList[i].checked = false;
      if (cooperateEnterpriseList[i].cooperateEnterpriseId == e.detail.value) {
        cooperateEnterpriseList[i].checked = true;
        cooperateEnterpriseId = cooperateEnterpriseList[i].cooperateEnterpriseId;
        cooperateEnterpriseName = cooperateEnterpriseList[i].cooperateEnterpriseName;
        customerName = cooperateEnterpriseList[i].contacts;
        customerAddress = cooperateEnterpriseList[i].address;
        customerPhone = cooperateEnterpriseList[i].telephone;
      }
    }
    self.setData({
      cooperateEnterpriseList: cooperateEnterpriseList,
      cooperateEnterpriseName: cooperateEnterpriseName,
      customerName: customerName,
      customerAddress: customerAddress,
      customerPhone: customerPhone,
    });

    let pages = getCurrentPages(); //当前页面
    let prevPage = pages[pages.length - 2]; //上一页面
    prevPage.setData({ //直接给上移页面赋值
      cooperateEnterpriseId: cooperateEnterpriseId,
      cooperateEnterpriseName: cooperateEnterpriseName,
      customerName: customerName,
      customerAddress: customerAddress,
      customerPhone: customerPhone,
    });
  },
  // 跳转上一页
  dosubmit(e) {
    wx.navigateBack({
      delta: 0,
    });
  },
  /** 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    let self = this;

    self.queryCooperateEnterpriseList(options.cooperateEnterpriseId);

  },
  // 查询合作企业列表
  queryCooperateEnterpriseList(cooperateEnterpriseId) {
    let self = this;

    let params = {};

    app.fetchApis(self, qCloud.api.common_queryCooperateEnterpriseList, params, "POST", function (e) {
      let result = e.data;

      if (result.code == 200) {
        let data = result.data;

        let cooperateEnterpriseList = data.enterpriseList;
        for (let i = 0; i < cooperateEnterpriseList.length; i++) {
          cooperateEnterpriseList[i].checked = false;
          if (cooperateEnterpriseId && cooperateEnterpriseList[i].cooperateEnterpriseId == cooperateEnterpriseId) {
            cooperateEnterpriseList[i].checked = true;
          }
        }

        self.setData({
          cooperateEnterpriseList: cooperateEnterpriseList
        });
      }
    });
  },
  /** 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {},
  /** 生命周期函数--监听页面显示 */
  onShow: function () {},
  /** 生命周期函数--监听页面隐藏 */
  onHide: function () {},
  /** 生命周期函数--监听页面卸载 */
  onUnload: function () {},
  /** 页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {},
  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function () {},
  /** 用户点击右上角分享 */
  onShareAppMessage: function () {}
});
